package com.zhang.designmode.wangzheng_design_mode.T18;

import org.slf4j.Logger;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: zhangxuzhuang
 * @DateTime: 2021/12/21 23:13
 * @Description: 每间隔一段时间自动更新数据
 */
public class ScheduledUpdate {
    private Updater updater;
    //间隔时间 秒
    private Long interval;
    public ScheduledUpdate() {
    }

    public ScheduledUpdate(Updater updater, Long interval) {
        this.updater = updater;
        this.interval = interval*1000;
    }
    void run(){
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(interval);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    updater.update();
                }
            }
        });
    }
}
